package hang.study.uistudy;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.format.DateUtils;
import android.util.Log;

import java.lang.ref.WeakReference;

/**
 * Created by hang on 2015-7-10.
 */
public class TimerService extends Service {

    private static final String TAG = "TimerService";
    public static int TIMER_NOTIFICATION = 0;

    private NotificationManager mNM = null;
    private Notification mNotification = null;
    private long mStart = 0;
    private long mTime = 0;

    private TimeHandler mHandler = null;

    private final IBinder mBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    @Override
    public void onCreate(){
        Log.i(TAG, "onCreate");
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        mHandler = new TimeHandler(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Log.i(TAG,"Received start id "+ startId+ " with intent "+intent);

        showNotification();
        return START_STICKY;
    }

    private void updateNotification(long time){
        String title = getResources().getString(R.string.running_timer_notification_title);
        String message = DateUtils.formatElapsedTime(time / 1000);

        Context context = getApplicationContext();
        Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        mNotification.setLatestEventInfo(context, title, message, pendingIntent);
        mNM.notify(TIMER_NOTIFICATION, mNotification);
    }

    private void updateTime(long time){
        // Broadcast the new time
        Intent intent = new Intent(MainActivity.ACTION_TIME_UPDATE);
        intent.putExtra("time", time);
        sendBroadcast(intent);
        //
        updateNotification(time);
    }

    private void showNotification(){
        mNotification = new Notification(R.mipmap.ic_launcher,null,System.currentTimeMillis());
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;
        startForeground(TIMER_NOTIFICATION,mNotification);
    }

    private static class TimeHandler extends Handler {
        WeakReference<TimerService> mServiceRef;

        public TimeHandler(TimerService service){
            mServiceRef = new WeakReference<TimerService>(service);
        }

        @Override
        public void handleMessage(Message msg){
            TimerService service = mServiceRef.get();
            if(service != null){
                long current = System.currentTimeMillis();
                service.mTime += current - service.mStart;
                service.mStart = current;

                service.updateTime(service.mTime);

                sendEmptyMessageDelayed(0,250);
            }
        }
    }

    public class LocalBinder extends Binder{

        TimerService getService(){
            return TimerService.this;
        }

    }
}
